package com.example.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.example.bean.OrderCondition;
import com.example.model.Order;
import com.example.model.OrderItem;
import com.example.model.Product;
import com.example.repository.OrderRepository;
import com.example.repository.ProductRepository;
import com.example.repository.UserRepository;
import com.example.service.OrderService;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    OrderRepository orderRepository;

    @Autowired
    ProductRepository productRepository;

    @Autowired
    OrderService orderService;


    /**
     * 分页检索订单
     * @param page 当前页码
     * @param size 一页件数
     * @return 订单信息
     */
    @RequestMapping(value="page", method=RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @Transactional
    public Page<Order> getOrderPage(int page, int size) {

        // 检索条件,测试用，实际中从前端获取
        OrderCondition orderCondition = new OrderCondition();
        //orderCondition.setOrderNum("ORD-100001");
        //orderCondition.setBuyNum(150);
        //orderCondition.setProductName("product-5");

        PageRequest pageRequest = new PageRequest(page, size, new Sort(Direction.DESC, "orderNum"));
        Page<Order> result = orderService.findOrderPage(orderCondition, pageRequest);

        return result;
    }

    /**
     * 查询订单
     * @return 订单列表
     */
    @RequestMapping(method=RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @Transactional
    public List<Order> getOrders() {
        return orderService.findOrderAll();
    }

    /**
     * 创建订单
     * @return
     */
    @RequestMapping(method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    @Transactional
    public Order postOrder() {

        Order creatingOrder = new Order("ORD-100009");
        Product product = productRepository.findByName("product-1");
        OrderItem orderItem = new OrderItem(1);
        orderItem.setProduct(product);
        creatingOrder.addOrderItem(orderItem);

        Order createdOrder = orderService.createOrder(creatingOrder);
        return createdOrder;
    }

    /**
     * 更新订单
     * @param orderNum
     * @return
     */
    @RequestMapping(value="{orderNum}", method=RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    @Transactional
    public Order putOrder(@PathVariable String orderNum) {
        Order updatingOrder = new Order();
        updatingOrder.setRecivePhone("13702156454");
        return orderService.updateOrder(orderNum, updatingOrder);
    }

    /**
     * 删除订单
     * @param orderNum
     */
    @RequestMapping(value="{orderNum}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @Transactional
    public void deleteOrder(@PathVariable String orderNum) {
        orderService.deleteOrder(orderNum);
    }

    /**
     * 订单数据初始化
     * @return 完成提示信息
     */
    @RequestMapping(value="/init")
    @Transactional
    public String insertDatas() {

        // Delete All
        orderRepository.deleteAll();
        productRepository.deleteAll();

        // Product Data
        Set<Product> pset = new HashSet<>();
        Product p1 = new Product("product-1", 10);
        Product p2 = new Product("product-2", 20);
        Product p3 = new Product("product-3", 20);
        Product p4 = new Product("product-4", 30);
        Product p5 = new Product("product-5", 50);
        pset.add(p1);
        pset.add(p2);
        pset.add(p3);
        pset.add(p4);
        pset.add(p5);
        productRepository.save(pset);

        // Order-1
        OrderItem orderItem1 = new OrderItem(1);
        orderItem1.setProduct(p1);
        OrderItem orderItem2 = new OrderItem(4);
        orderItem2.setProduct(p2);
        OrderItem orderItem3 = new OrderItem(7);
        orderItem3.setProduct(p3);
        Order order = new Order("ORD-100001");
        order.addOrderItem(orderItem1);
        order.addOrderItem(orderItem2);
        order.addOrderItem(orderItem3);
        double money = 0;
        for (OrderItem item : order.getOrderItems()) {
            money += item.getBuyNum() * item.getProduct().getPrice();
        }
        order.setMoney(money);
        orderRepository.save(order);

        // Order-2
        orderItem1 = new OrderItem(2);
        orderItem1.setProduct(p2);
        orderItem2 = new OrderItem(5);
        orderItem2.setProduct(p4);
        orderItem3 = new OrderItem(8);
        orderItem3.setProduct(p5);
        order = new Order("ORD-100002");
        order.addOrderItem(orderItem1);
        order.addOrderItem(orderItem2);
        order.addOrderItem(orderItem3);
        money = 0;
        for (OrderItem item : order.getOrderItems()) {
            money += item.getBuyNum() * item.getProduct().getPrice();
        }
        order.setMoney(money);
        orderRepository.save(order);

        // Order-3
        orderItem1 = new OrderItem(3);
        orderItem1.setProduct(p4);
        orderItem2 = new OrderItem(6);
        orderItem2.setProduct(p1);
        orderItem3 = new OrderItem(9);
        orderItem3.setProduct(p2);
        order = new Order("ORD-100003");
        order.addOrderItem(orderItem1);
        order.addOrderItem(orderItem2);
        order.addOrderItem(orderItem3);
        money = 0;
        for (OrderItem item : order.getOrderItems()) {
            money += item.getBuyNum() * item.getProduct().getPrice();
        }
        order.setMoney(money);

        orderRepository.save(order);

        return "Data init completed!";
    }
}
